package com.geek.novel.dialog

import android.content.Context
import android.content.Intent
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import com.geek.novel.R
import com.geek.novel.utils.CommUtil


/**
 * 基础的弹框
 */
open class BaseDialog(val context: Context, private val layoutId: Int) {

    private lateinit var dialogView: View
    lateinit var dialog: PopupWindow

    open fun onCreateView(){
		this.init()
        this.initDialog()
        this.bindEvent(dialogView)
    }

    private fun init(){
        //加载弹出框的布局
        dialogView = LayoutInflater.from(this.context).inflate(layoutId, null)

        dialog = PopupWindow(dialogView, this.dialogLayoutWidth(), this.dialogLayoutHeight())

        dialog.isFocusable = true// 取得焦点
        //注意  要是点击外部空白处弹框消息  那么必须给弹框设置一个背景色  不然是不起作用的
        dialog.setBackgroundDrawable(this.dialogBackgroundDrawable())

        //点击外部消失
//        dialog.isOutsideTouchable = true
        //设置可以点击
        dialog.isTouchable = true
        //进入退出的动画，指定刚才定义的style
        dialog.animationStyle = this.animationStyle()

    }

    /**弹框的布局宽度*/
    open fun dialogLayoutWidth(): Int{
        return ViewGroup.LayoutParams.WRAP_CONTENT
    }
    /**弹框的布局高度*/
    open fun dialogLayoutHeight(): Int{
        return ViewGroup.LayoutParams.WRAP_CONTENT
    }
    /**弹框的背景*/
    open fun dialogBackgroundDrawable(): Drawable?{
        return ColorDrawable()
    }

    /**动画样式*/
    open fun animationStyle(): Int{
        return R.style.bottom_dialog_anim_style
    }

    /**初始化弹框*/
    open fun initDialog(){
    }

    /**绑定事件*/
    open fun bindEvent(dialogView: View){
    }

    open fun openDialog(gravity: Int){
        this.showAtLocation(gravity, 0, 0)
    }
    open fun openDialog(gravity: Int, event: MotionEvent?){
        /**触屏的X坐标*/
        var touchX = 0F
        /**触屏的Y坐标*/
        var touchY = 0F
        if(event != null){
            touchX = event.x
            touchY = event.y
        }
        openDialog(gravity, touchX, touchY)
    }
    open fun openDialog(gravity: Int, x: Float, y: Float){
        //弹出框的X坐标修正
        var offsetX = x.toInt()
        val offsetY = y.toInt()

        offsetX -= dialogView.width/2

        this.showAtLocation(gravity, offsetX, offsetY)
    }

    private fun showAtLocation(gravity: Int, x: Int, y: Int){
        if(CommUtil.isActivityActive(context)){
            dialog.showAtLocation(dialogView, gravity, x, y)
        }
    }

    fun isShowing(): Boolean{
        return dialog.isShowing
    }

    fun dismiss() {
        return dialog.dismiss()
    }

    /**监听关闭*/
    open fun setOnDismissListener(onDismissListener: PopupWindow.OnDismissListener){
        this.dialog.setOnDismissListener(onDismissListener)
    }

    open fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {}

}